// pages/newsdetails/newsdetails.js
const app = getApp()
var server = require('../../utils/server');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,//控制上滑距离
    windowHeight: 0,//页面高度
    tu: app.globalData.tupian,
    name: '',
    toheadpicpath: '',
    toid: '',
    conversationid: '',
    list: [],
    content: '',
    PageIndex: 1,
    count: 0,
    hasNext: false,
    touch:false, //触摸
    id:'',
    endindex:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    that = this
    console.log(e)
    wx.setNavigationBarTitle({
      title: e.name
    })
    if (e.conversationid) {
      that.huihua(e.conversationid, 1, 1)
    } else {

    }
    that.setData({
      name: e.name,
      toheadpicpath: e.toheadpicpath,
      toid: e.toid,
      conversationid: e.conversationid
    })
  },



  //长按点击区分
  touchStart: function (e) {
    console.log('触摸开始', e)
    var id = e.currentTarget.dataset.id
    var endindex = e.currentTarget.dataset.index
    that.setData({
      touch:false,
      id:id,
      endindex:endindex
    })
  },
  touchEnd: function (e) {
    console.log('触摸结束', e)
    if (!that.data.touch ) {
      console.log('11')
    } else {
      console.log('22')
      that.delete()
    }
  },
  //长按事件
  bingLongTap: function (e) {
    console.log('长按')
    that.setData({
      touch:true
    })

  },


  //删除消息
  delete:function(e){
    var id = that.data.id
    console.log('endindex',that.data.endindex)
    wx.showModal({
      title: '是否删除此条消息',
      // content: '模态弹窗',
      success: function (res) {
        if (res.confirm) {
          var uri = 'message/' + id +'?conversationId='+ that.data.conversationid

          server.requestForm(false, 'delete', uri, {
          }, function (e) {
            console.log('删除', e)
            if (e.code == 0) {
              var list = that.data.list
              list.splice(that.data.endindex, 1)
              that.setData({
                list: list,
              })
              // that.onShow()
            } else {
              wx.showToast({
                title: e.message,
                icon: 'none',
                duration: 1000,
              });
            }
            // that.onShow()
          })
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },

  gundong: function (e) {
    var height = wx.getSystemInfoSync().windowHeight;
    console.log('height', height)
    wx.pageScrollTo({
      scrollTop: height,
      duration: 300,
    })
  },
  huihua: function (conversationID, PageIndex, gundong) {
    server.requestForm(false, 'get', 'message', {
      page: {
        size: 10,
        index: PageIndex
      },
      filter: {
        conversationID: conversationID
      },
      sort: {
        createTime: -1
      }
    },
      function (res) {
        if (res.code == 0) {
          var list = that.data.list
          if (gundong == 1) {
            res.data.items.reverse()
            for (var i = 0; i < res.data.items.length; i++) {
              res.data.items[i].wo = 0
              var str = res.data.items[i].createTime
              var d = new Date(str.replace(/-/g, '/'));
              // var d = new Date(res.data.items[i].createTime);
              if (d.getMinutes() < 10) {
                var Minutes = '0' + d.getMinutes()
              } else {
                var Minutes = d.getMinutes()
              }
              res.data.items[i].createTime = (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + Minutes;
              if (app.globalData.userID == res.data.items[i].fromID) {
                res.data.items[i].wo = 1
              }
              list.push(res.data.items[i])
            }
          } else {
            for (var i = 0; i < res.data.items.length; i++) {
              res.data.items[i].wo = 0
              var str = res.data.items[i].createTime
              var d = new Date(res.data.items[i].createTime);
              if (d.getMinutes() < 10) {
                var Minutes = '0' + d.getMinutes()
              } else {
                var Minutes = d.getMinutes()
              }
              res.data.items[i].createTime = (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + Minutes;
              if (app.globalData.userID == res.data.items[i].fromID) {
                res.data.items[i].wo = 1
              }
              list.unshift(res.data.items[i])
            }
          }
          console.log('list', list)
          that.setData({
            list: list,
            hasNext: res.data.hasNext
          })
          if (gundong == 1) {
            that.gundong()
          }
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 1000,
          });
        }

      })
  },
  content: function (e) {
    that.setData({
      content: e.detail.value
    })
  },
  bindconfirm: function (e) {
    console.log(e, app.globalData)
    if(that.data.content==''){
      wx.showToast({
        title: '请输入内容',
        icon: 'none',
        duration: 1000,
      });
    }else{
      server.requestForm(false, 'post', 'message', {
        fromID: app.globalData.userID,
        fromNickname: app.globalData.userInfo.nickName,
        fromHeadPicPath: app.globalData.userInfo.avatarUrl,
        toID: that.data.toid,
        toNickname: that.data.name,
        toHeadPicPath: that.data.toheadpicpath,
        content: that.data.content
      },
        function (res) {
          console.log('res', res)
          that.setData({
            content: '',
            list: [],
            PageIndex: 1
          })
          that.huihua(res.data.conversationID, 1, 1)
        })
    }

  },
  shangye: function (e) {
    if (that.data.hasNext) {
      that.setData({
        PageIndex: that.data.PageIndex + 1
      })
      that.huihua(that.data.conversationid, that.data.PageIndex, 0)
    }
  }
})